<template>
  <el-dialog
    :title="!dataForm.num ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="账号" prop="userid">
      <el-input v-model="dataForm.userid" placeholder="账号"></el-input>
    </el-form-item>
<!--    <el-form-item label="人员类型，1司机0助手" prop="label">-->
<!--      <el-input v-model="dataForm.label" placeholder="人员类型，1司机0助手"></el-input>-->
<!--    </el-form-item>-->
      <el-form-item label="人员类型" prop="lable">
        <el-select v-model="dataForm.label" placeholder="人员类型">
          <el-option key="0" label="助手" value="0"></el-option>
          <el-option key="1" label="司机" value="1"></el-option>
        </el-select>
        <!--      <el-input v-model="dataForm.classification" placeholder="人员类型（司机1，助手0）"></el-input>-->
      </el-form-item>
    <el-form-item label="试卷编号" prop="examnum">
      <el-input v-model="dataForm.examnum" placeholder="试卷编号"></el-input>
    </el-form-item>
    <el-form-item label="考试时间" prop="time">
      <el-input v-model="dataForm.time" placeholder="考试时间"></el-input>
    </el-form-item>
    <el-form-item label="用时" prop="usetime">
      <el-input v-model="dataForm.usetime" placeholder="用时"></el-input>
    </el-form-item>
    <el-form-item label="成绩" prop="score">
      <el-input v-model="dataForm.score" placeholder="成绩"></el-input>
    </el-form-item>
<!--    <el-form-item label="通过（是1否0）" prop="ispass">-->
<!--      <el-input v-model="dataForm.ispass" placeholder="通过（是1否0）"></el-input>-->
<!--    </el-form-item>-->
      <el-form-item label="通过" prop="ispass">
        <el-select v-model="dataForm.ispass" placeholder="是否通过">
          <el-option key="0" label="否" value="0"></el-option>
          <el-option key="1" label="通过" value="1"></el-option>
        </el-select>
        <!--      <el-input v-model="dataForm.classification" placeholder="人员类型（司机1，助手0）"></el-input>-->
      </el-form-item>
    <el-form-item label="有效期" prop="value">
      <el-input v-model="dataForm.value" placeholder="有效期"></el-input>
    </el-form-item>
    <el-form-item label="试卷url" prop="picture">
      <el-input v-model="dataForm.picture" placeholder="试卷url"></el-input>
    </el-form-item>
    <el-form-item label="签名url" prop="namepicture">
      <el-input v-model="dataForm.namepicture" placeholder="签名url"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          num: 0,
          userid: '',
          label: '',
          examnum: '',
          time: '',
          usetime: '',
          score: '',
          ispass: '',
          value: '',
          picture: '',
          namepicture: ''
        },
        dataRule: {
          userid: [
            { required: true, message: '账号不能为空', trigger: 'blur' }
          ],
          examnum: [
            { required: true, message: '试卷编号不能为空', trigger: 'blur' }
          ],
          score: [
            { required: true, message: '成绩不能为空', trigger: 'blur' }
          ],
          ispass: [
            { required: true, message: '通过（是1否0）不能为空', trigger: 'blur' }
          ],
          value: [
            { required: true, message: '有效期不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.num = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.num) {
            this.$http({
              url: this.$http.adornUrl(`/PCguanli/examregistration/info/${this.dataForm.num}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userid = data.examRegistration.userid
                this.dataForm.label = data.examRegistration.label
                this.dataForm.examnum = data.examRegistration.examnum
                this.dataForm.time = data.examRegistration.time
                this.dataForm.usetime = data.examRegistration.usetime
                this.dataForm.score = data.examRegistration.score
                this.dataForm.ispass = data.examRegistration.ispass
                this.dataForm.value = data.examRegistration.value
                this.dataForm.picture = data.examRegistration.picture
                this.dataForm.namepicture = data.examRegistration.namepicture
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/PCguanli/examregistration/${!this.dataForm.num ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'num': this.dataForm.num || undefined,
                'userid': this.dataForm.userid,
                'label': this.dataForm.label,
                'examnum': this.dataForm.examnum,
                'time': this.dataForm.time,
                'usetime': this.dataForm.usetime,
                'score': this.dataForm.score,
                'ispass': this.dataForm.ispass,
                'value': this.dataForm.value,
                'picture': this.dataForm.picture,
                'namepicture': this.dataForm.namepicture
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
